home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 November / november_2001.iso / Browsers / Netscape 6.1 / browser.xpi / bin / components / nsProxyAutoConfig.js < prev    next >
Encoding:
Text File  |  2001-06-11  |  13.1 KB  |  395 lines

  1. /* -*- Mode: Java; tab-width: 4; c-basic-offset: 4; -*-
  2.  * 
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is mozilla.org code.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation.  Portions created by Netscape are
  17.  * Copyright (C) 1998 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s): 
  21.  *   Akhil Arora <akhil.arora@sun.com>
  22.  *   Tomi Leppikangas <Tomi.Leppikangas@oulu.fi>
  23.  */
  24.  
  25. /*
  26.    Script for Proxy Auto Config in the new world order.
  27.        - Gagan Saksena 04/24/00 
  28. */
  29.  
  30. const kPAC_CONTRACTID = "@mozilla.org/network/proxy_autoconfig;1";
  31. const kIOSERVICE_CONTRACTID = "@mozilla.org/network/io-service;1";
  32. const kDNS_CONTRACTID = "@mozilla.org/network/dns-service;1";
  33. const kPAC_CID = Components.ID("{63ac8c66-1dd2-11b2-b070-84d00d3eaece}");
  34. const nsIProxyAutoConfig = Components.interfaces.nsIProxyAutoConfig;
  35. const nsIIOService = Components.interfaces['nsIIOService'];
  36. const nsIDNSService = Components.interfaces.nsIDNSService;
  37.  
  38. // implementor of nsIProxyAutoConfig
  39. function nsProxyAutoConfig() {};
  40.  
  41. // global variable that will hold the downloaded js 
  42. var pac = null;
  43. //hold PAC's URL, used in evalAsCodebase()
  44. var pacURL;
  45. // ptr to eval'ed FindProxyForURL function
  46. var LocalFindProxyForURL=null;
  47. // sendbox in which we eval loaded autoconfig js file
  48. var ProxySandBox = null;
  49.  
  50. nsProxyAutoConfig.prototype = {
  51.     sis: null,
  52.     done: false,
  53.  
  54.     ProxyForURL: function(url, host, port, type) {
  55.         /* If we're not done loading the pac yet, wait (ideally). For
  56.            now, just return DIRECT to avoid loops. A simple mutex
  57.            between ProxyForURL and LoadPACFromURL locks-up the
  58.            browser. */
  59.         if (!this.done) {
  60.             host.value = null;
  61.             type.value = "direct";
  62.             return;
  63.         }
  64.  
  65.         var uri = url.QueryInterface(Components.interfaces.nsIURI);
  66.         // Call the original function-
  67.         var proxy = LocalFindProxyForURL(uri.spec, uri.host);
  68.         if (proxy == "DIRECT") {
  69.             host.value = null;
  70.             type.value = "direct";
  71.         }
  72.         else {
  73.             // TODO warn about SOCKS
  74.  
  75.             // we ignore everything else past the first proxy. 
  76.             // we could theoretically check isResolvable now and continue 
  77.             // parsing. but for now...
  78.             var hostport = /^PROXY ([^:]+):(\d+)/(proxy);
  79.             host.value = hostport[1];
  80.             port.value = hostport[2];
  81.             type.value = "http"; //proxy (http, socks, direct, etc)
  82.         }
  83.     },
  84.  
  85.     LoadPACFromURL: function(uri, ioService) {
  86.         this.done = false;
  87.         var channel = ioService.newChannelFromURI(uri);
  88.         pacURL = uri.spec;
  89.         channel.asyncOpen(this, null);
  90.     },
  91.  
  92.     // nsIStreamListener interface
  93.     onStartRequest: function(request, ctxt) { 
  94.         pac = '';
  95.         LocalFindProxyForURL=null;
  96.         this.sis = 
  97.         Components.Constructor('@mozilla.org/scriptableinputstream;1',
  98.                                'nsIScriptableInputStream', 
  99.                                'init');
  100.     },
  101.  
  102.     onStopRequest: function(request, ctxt, status, errorMsg) {
  103.         this.done = true;
  104.         if(!ProxySandBox) {
  105.            ProxySandBox = new Sandbox();
  106.         }
  107.         // add predefined functions to pac
  108.         var mypac = pacUtils + pac;
  109.         // evaluate loded js file
  110.         evalInSandbox(mypac, ProxySandBox, pacURL);
  111.         ProxySandBox.myIP = dns.myIPAddress;
  112.         LocalFindProxyForURL=ProxySandBox.FindProxyForURL;
  113.     },
  114.  
  115.     onDataAvailable: function(request, ctxt, inStream, sourceOffset, count) {
  116.         var ins = new this.sis(inStream);
  117.         pac += ins.read(count);
  118.     }
  119. }
  120.  
  121. var pacModule = new Object();
  122.  
  123. pacModule.registerSelf =
  124.     function (compMgr, fileSpec, location, type) {
  125.         compMgr.registerComponentWithType(kPAC_CID,
  126.             "Proxy Auto Config",
  127.             kPAC_CONTRACTID,
  128.             fileSpec, location, 
  129.             true, true, type);
  130.     }
  131.  
  132. pacModule.getClassObject =
  133. function (compMgr, cid, iid) {
  134.         if (!cid.equals(kPAC_CID))
  135.             throw Components.results.NS_ERROR_NO_INTERFACE;
  136.  
  137.         if (!iid.equals(Components.interfaces.nsIFactory))
  138.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  139.  
  140.         return pacFactory;
  141.     }
  142.  
  143. pacModule.canUnload =
  144.     function (compMgr) {
  145.         return true;
  146.     }
  147.  
  148. var pacFactory = new Object();
  149. pacFactory.createInstance =
  150.     function (outer, iid) {
  151.         if (outer != null)
  152.             throw Components.results.NS_ERROR_NO_AGGREGATION;
  153.  
  154.         if (!iid.equals(nsIProxyAutoConfig) &&
  155.             !!iid.equals(Components.interfaces.nsIStreamListener) &&
  156.             !iid.equals(Components.interfaces.nsISupports)) {
  157.             // shouldn't this be NO_INTERFACE?
  158.             throw Components.results.NS_ERROR_INVALID_ARG;
  159.         }
  160.         return PacMan;
  161.     }
  162.  
  163. function NSGetModule(compMgr, fileSpec) {
  164.     return pacModule;
  165. }
  166.  
  167. var PacMan = new nsProxyAutoConfig() ;
  168.  
  169. var ios = Components.classes[kIOSERVICE_CONTRACTID].getService(nsIIOService);
  170. var dns = Components.classes[kDNS_CONTRACTID].getService(nsIDNSService);
  171.  
  172. var pacUtils = 
  173. "function dnsDomainIs(host, domain) {\n" +
  174. "    return (host.length >= domain.length &&\n" +
  175. "            host.substring(host.length - domain.length) == domain);\n" +
  176. "}\n" +
  177.  
  178. "function dnsDomainLevels(host) {\n" +
  179. "    return host.split('.').length-1;\n" +
  180. "}\n" +
  181.  
  182. "function convert_addr(ipchars) {\n"+
  183. "    var bytes = ipchars.split('.');\n"+
  184. "    var result = ((bytes[0] & 0xff) << 24) |\n"+
  185. "                 ((bytes[1] & 0xff) << 16) |\n"+
  186. "                 ((bytes[2] & 0xff) <<  8) |\n"+
  187. "                  (bytes[3] & 0xff);\n"+
  188. "    return result;\n"+
  189. "}\n"+
  190.  
  191. "function isInNet(ipaddr, pattern, maskstr) {\n"+
  192. "    var test = /^(\\d{1,4})\\.(\\d{1,4})\\.(\\d{1,4})\\.(\\d{1,4})$/(ipaddr);\n"+
  193. "    if (test == null) {\n"+
  194. "        ipaddr = dnsResolve(ipaddr);\n"+
  195. "    } else if (test[1] > 255 || test[2] > 255 || \n"+
  196. "               test[3] > 255 || test[4] > 255) {\n"+
  197. "        return false;    // not an IP address\n"+
  198. "    }\n"+
  199. "    var host = convert_addr(ipaddr);\n"+
  200. "    var pat  = convert_addr(pattern);\n"+
  201. "    var mask = convert_addr(maskstr);\n"+
  202. "    return ((host & mask) == (pat & mask));\n"+
  203. "    \n"+
  204. "}\n"+
  205.  
  206. "function isPlainHostName(host) {\n" +
  207. "    return (host.search('\\.') == -1);\n" +
  208. "}\n" +
  209.  
  210. "function isResolvable(host) {\n" +
  211. "    var ip = dnsResolve(host);\n" +
  212. "    return (ip != null);\n" +
  213. "}\n" +
  214.  
  215. "function localHostOrDomainIs(host, hostdom) {\n" +
  216. "    if (isPlainHostName(host)) {\n" +
  217. "        return (hostdom.search('/^' + host + '/') != -1);\n" +
  218. "    }\n" +
  219. "    else {\n" +
  220. "        return (host == hostdom); //TODO check \n" +
  221. "    }\n" +
  222. "}\n" +
  223.  
  224. " var myIP;\n" +
  225. "function myIpAddress() {\n" +
  226. "    return (myIP) ? myIP : '127.0.0.1';\n" +
  227. "}\n" +
  228.  
  229. "function shExpMatch(url, pattern) {\n" +
  230. "   pattern = pattern.replace(/\\./g, '\\\\.');\n" +
  231. "   pattern = pattern.replace(/\\*/g, '.*');\n" +
  232. "   pattern = pattern.replace(/\\?/g, '.');\n" +
  233. "   var newRe = new RegExp(pattern);\n" +
  234. "   return newRe.test(url);\n" +
  235. "}\n" +
  236.  
  237. "var wdays = new Array('SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT');\n" +
  238.  
  239. "var monthes = new Array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC');\n"+
  240.  
  241. "function weekdayRange() {\n" +
  242. "    function getDay(weekday) {\n" +
  243. "        for (var i = 0; i < 6; i++) {\n" +
  244. "            if (weekday == wdays[i]) \n" +
  245. "                return i;\n" +
  246. "        }\n" +
  247. "        return -1;\n" +
  248. "    }\n" +
  249. "    var date = new Date();\n" +
  250. "    var argc = arguments.length;\n" +
  251. "    var wday;\n" +
  252. "    if (argc < 1)\n" +
  253. "        return false;\n" +
  254. "    if (arguments[argc - 1] == 'GMT') {\n" +
  255. "        argc--;\n" +
  256. "        wday = date.getUTCDay();\n" +
  257. "    } else {\n" +
  258. "        wday = date.getDay();\n" +
  259. "    }\n" +
  260. "    var wd1 = getDay(arguments[0]);\n" +
  261. "    var wd2 = (argc == 2) ? getDay(arguments[1]) : wd1;\n" +
  262. "    return (wd1 == -1 || wd2 == -1) ? false\n" +
  263. "                                    : (wd1 <= wday && wday <= wd2);\n" +
  264. "}\n" +
  265.  
  266. "function dateRange() {\n" +
  267. "    function getMonth(name) {\n" +
  268. "        for (var i = 0; i < 6; i++) {\n" +
  269. "            if (name == monthes[i])\n" +
  270. "                return i;\n" +
  271. "        }\n" +
  272. "        return -1;\n" +
  273. "    }\n" +
  274. "    var date = new Date();\n" +
  275. "    var argc = arguments.length;\n" +
  276. "    if (argc < 1) {\n" +
  277. "        return false;\n" +
  278. "    }\n" +
  279. "    var isGMT = (arguments[argc - 1] == 'GMT');\n" +
  280. "\n" +
  281. "    if (isGMT) {\n" +
  282. "        argc--;\n" +
  283. "    }\n" +
  284. "    // function will work even without explict handling of this case\n" +
  285. "    if (argc == 1) {\n" +
  286. "        var tmp = parseInt(arguments[0]);\n" +
  287. "        if (isNaN(tmp)) {\n" +
  288. "            return ((isGMT ? date.getUTCMonth() : date.getMonth()) ==\n" +
  289. "getMonth(arguments[0]));\n" +
  290. "        } else if (tmp < 32) {\n" +
  291. "            return ((isGMT ? date.getUTCDate() : date.getDate()) == tmp);\n" +
  292. "        } else { \n" +
  293. "            return ((isGMT ? date.getUTCFullYear() : date.getFullYear()) ==\n" +
  294. "tmp);\n" +
  295. "        }\n" +
  296. "    }\n" +
  297. "    var year = date.getFullYear();\n" +
  298. "    var date1, date2;\n" +
  299. "    date1 = new Date(year,  0,  1,  0,  0,  0);\n" +
  300. "    date2 = new Date(year, 11, 31, 23, 59, 59);\n" +
  301. "    var adjustMonth = false;\n" +
  302. "    for (var i = 0; i < (argc >> 1); i++) {\n" +
  303. "        var tmp = parseInt(arguments[i]);\n" +
  304. "        if (isNaN(tmp)) {\n" +
  305. "            var mon = getMonth(arguments[i]);\n" +
  306. "            date1.setMonth(mon);\n" +
  307. "        } else if (tmp < 32) {\n" +
  308. "            adjustMonth = (argc <= 2);\n" +
  309. "            date1.setDate(tmp);\n" +
  310. "        } else {\n" +
  311. "            date1.setFullYear(tmp);\n" +
  312. "        }\n" +
  313. "    }\n" +
  314. "    for (var i = (argc >> 1); i < argc; i++) {\n" +
  315. "        var tmp = parseInt(arguments[i]);\n" +
  316. "        if (isNaN(tmp)) {\n" +
  317. "            var mon = getMonth(arguments[i]);\n" +
  318. "            date2.setMonth(mon);\n" +
  319. "        } else if (tmp < 32) {\n" +
  320. "            date2.setDate(tmp);\n" +
  321. "        } else {\n" +
  322. "            date2.setFullYear(tmp);\n" +
  323. "        }\n" +
  324. "    }\n" +
  325. "    if (adjustMonth) {\n" +
  326. "        date1.setMonth(date.getMonth());\n" +
  327. "        date2.setMonth(date.getMonth());\n" +
  328. "    }\n" +
  329. "    if (isGMT) {\n" +
  330. "    var tmp = date;\n" +
  331. "        tmp.setFullYear(date.getUTCFullYear());\n" +
  332. "        tmp.setMonth(date.getUTCMonth());\n" +
  333. "        tmp.setDate(date.getUTCDate());\n" +
  334. "        tmp.setHours(date.getUTCHours());\n" +
  335. "        tmp.setMinutes(date.getUTCMinutes());\n" +
  336. "        tmp.setSeconds(date.getUTCSeconds());\n" +
  337. "        date = tmp;\n" +
  338. "    }\n" +
  339. "    return ((date1 <= date) && (date <= date2));\n" +
  340. "}\n" +
  341.  
  342. "function timeRange() {\n" +
  343. "    var argc = arguments.length;\n" +
  344. "    var date = new Date();\n" +
  345. "    var isGMT= false;\n"+
  346. "\n" +
  347. "    if (argc < 1) {\n" +
  348. "        return false;\n" +
  349. "    }\n" +
  350. "    if (arguments[argc - 1] == 'GMT') {\n" +
  351. "        isGMT = true;\n" +
  352. "        argc--;\n" +
  353. "    }\n" +
  354. "\n" +
  355. "    var hour = isGMT ? date.getUTCHours() : date.getHours();\n" +
  356. "    var date1, date2;\n" +
  357. "    date1 = new Date();\n" +
  358. "    date2 = new Date();\n" +
  359. "\n" +
  360. "    if (argc == 1) {\n" +
  361. "        return (hour == arguments[0]);\n" +
  362. "    } else if (argc == 2) {\n" +
  363. "        return ((arguments[0] <= hour) && (hour <= arguments[1]));\n" +
  364. "    } else {\n" +
  365. "        switch (argc) {\n" +
  366. "        case 6:\n" +
  367. "            date1.setSeconds(arguments[2]);\n" +
  368. "            date2.setSeconds(arguments[5]);\n" +
  369. "        case 4:\n" +
  370. "            var middle = argc >> 1;\n" +
  371. "            date1.setHours(arguments[0]);\n" +
  372. "            date1.setMinutes(arguments[1]);\n" +
  373. "            date2.setHours(arguments[middle]);\n" +
  374. "            date2.setMinutes(arguments[middle + 1]);\n" +
  375. "            if (middle == 2) {\n" +
  376. "                date2.setSeconds(59);\n" +
  377. "            }\n" +
  378. "            break;\n" +
  379. "        default:\n" +
  380. "          throw 'timeRange: bad number of arguments'\n" +
  381. "        }\n" +
  382. "    }\n" +
  383. "\n" +
  384. "    if (isGMT) {\n" +
  385. "        date.setFullYear(date.getUTCFullYear());\n" +
  386. "        date.setMonth(date.getUTCMonth());\n" +
  387. "        date.setDate(date.getUTCDate());\n" +
  388. "        date.setHours(date.getUTCHours());\n" +
  389. "        date.setMinutes(date.getUTCMinutes());\n" +
  390. "        date.setSeconds(date.getUTCSeconds());\n" +
  391. "    }\n" +
  392. "    return ((date1 <= date) && (date <= date2));\n" +
  393. "}\n"
  394.  
  395.